package ckw.gui.wizards;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import ckw.database.DatabaseManager;
import ckw.database.VotersList;

public class WzdElectionDlgVotersListAdd extends Dialog {
	private VotersList votersList;
	private boolean edit;
	
	private Text txtNazwa;
	private Text txtOpis;
	private Text txtPlik;

	/**
	 * Create the dialog.
	 * 
	 * @param parentShell
	 * @param votersListList 
	 */
	public WzdElectionDlgVotersListAdd(Shell parentShell, VotersList votersList, boolean edit) {
		super(parentShell);
		
		this.votersList = votersList;
		this.edit = edit;
	}

	/**
	 * Create contents of the dialog.
	 * 
	 * @param parent
	 */
	@Override
	protected Control createDialogArea(Composite parent) {
		Composite container = (Composite) super.createDialogArea(parent);
		container.setLayout(new GridLayout(3, false));

		Label lblNazwa = new Label(container, SWT.NONE);
		lblNazwa.setText("Nazwa:");

		txtNazwa = new Text(container, SWT.BORDER);
		txtNazwa.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
				2, 1));
		txtNazwa.setText(!edit ? "" : votersList.getName());

		Label lblOpis = new Label(container, SWT.NONE);
		lblOpis.setText("Opis:");

		txtOpis = new Text(container, SWT.BORDER);
		txtOpis.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
				2, 1));
		txtOpis.setText(!edit ? "" : votersList.getDescription());

		Label label = new Label(container, SWT.SEPARATOR | SWT.HORIZONTAL);
		GridData gd_label = new GridData(SWT.FILL, SWT.CENTER, false, false, 3,
				1);
		gd_label.widthHint = 388;
		label.setLayoutData(gd_label);

		Label lblPlik = new Label(container, SWT.NONE);
		lblPlik.setText("Plik:");

		txtPlik = new Text(container, SWT.BORDER);
		txtPlik.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
				1, 1));

		Button btnWybierz = new Button(container, SWT.NONE);
		btnWybierz.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				org.eclipse.swt.widgets.FileDialog fileDialog = new org.eclipse.swt.widgets.FileDialog(
						new Shell());

				String[] filterExtensions = new String[] { "*.xml", "*" };

				fileDialog.setFilterExtensions(filterExtensions);
				String abba = fileDialog.open();

				if (abba != null) {
					StringBuilder sb = new StringBuilder(abba);
					txtPlik.setText(sb.toString());
				}
			}
		});
		btnWybierz.setText("Wybierz...");

		return container;
	}

	/**
	 * Create contents of the button bar.
	 * 
	 * @param parent
	 */
	@Override
	protected void createButtonsForButtonBar(Composite parent) {
		createButton(parent, IDialogConstants.OK_ID, edit ? "Zapisz" : "Dodaj",
				true);
		createButton(parent, IDialogConstants.CANCEL_ID,
				"Anuluj", false);
	}

	/**
	 * Return the initial size of the dialog.
	 */
	@Override
	protected Point getInitialSize() {
		return new Point(450, 300);
	}
	
	
	@Override
	protected void okPressed() {
		votersList.setName(txtNazwa.getText());
		votersList.setDescription(txtOpis.getText());
		
		// TODO: dodac odczyt z pliku, obsluge on OK click, zeby dodac do votersListList
		if(!edit) {
			DatabaseManager.getInstance().save(votersList);
		} else {
			DatabaseManager.getInstance().update(votersList);
		}
		
		
		super.okPressed();
	}
	
}
